//
//  AddLeaveView.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/11/2.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "AddLeaveView.h"

@implementation AddLeaveView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = BackGroundColor;
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        /**
         请假事由
         */
        CommonInputView *reasonInputView = [[CommonInputView alloc] init];
        reasonInputView.titleLabel.text = @"请假事由*";
        reasonInputView.titleTF.placeholder = @"请输入请假事由";
        [scrollView addSubview:reasonInputView];
        self.reasonInputView = reasonInputView;
        
        /**
         开始日期
         */
        CommonBtnView *startTimeView = [[CommonBtnView alloc] init];
        startTimeView.titleLabel.text = @"开始日期*";
        startTimeView.choseLabel.text = @"请选择";
        startTimeView.choseLabel.textColor = PlaceHoldColor;
        [scrollView addSubview:startTimeView];
        self.startTimeView = startTimeView;
        
        /**
         结束日期
         */
        CommonBtnView *endTimeView = [[CommonBtnView alloc] init];
        endTimeView.titleLabel.text = @"结束日期*";
        endTimeView.choseLabel.text = @"请选择";
        endTimeView.choseLabel.textColor = PlaceHoldColor;
        [scrollView addSubview:endTimeView];
        self.endTimeView = endTimeView;
        
        /**
         是否收费
         */
        CommentSwitchView *chargeSwitchView = [[CommentSwitchView alloc] init];
        [chargeSwitchView.titleSwitch addTarget:self action:@selector(chargeSwitchAction:) forControlEvents:UIControlEventValueChanged];   // 开关事件切换通知
        chargeSwitchView.titleLabel.text = @"是否收费";
        [scrollView addSubview:chargeSwitchView];
        self.chargeSwitchView = chargeSwitchView;
        
        /**
         备注
         */
        CommonInputView *remarkBtnView = [[CommonInputView alloc] init];
        remarkBtnView.titleLabel.text = @"备注";
        remarkBtnView.titleTF.placeholder = @"请输入备注信息，选填";
        [scrollView addSubview:remarkBtnView];
        self.remarkBtnView = remarkBtnView;
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(self);
    }];
    
    /**
     请假事由
     */
    [self.reasonInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.width.mas_equalTo(self.scrollView);
    }];
    
    /**
     开始日期
     */
    [self.startTimeView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.reasonInputView.mas_bottom);
        make.left.width.mas_equalTo(self);
    }];
    
    /**
     结束日期
     */
    [self.endTimeView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.startTimeView.mas_bottom);
        make.left.width.mas_equalTo(self);
    }];
    
    /**
     是否收费
     */
    [self.chargeSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.endTimeView.mas_bottom).offset(10);
        make.left.width.mas_equalTo(self);
    }];
    
    if (self.chargeSwitchView.titleSwitch.isOn) {
    
        /**
         收费
         */
        [self.commonInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.chargeSwitchView.mas_bottom);
            make.left.width.mas_equalTo(self);
        }];
        
        
        /**
         支付方式
         */
        [self.payStyleBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.commonInputView.mas_bottom);
            make.left.width.mas_equalTo(self);
        }];
        
        /**
         备注
         */
        [self.remarkBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.payStyleBtnView.mas_bottom);
            make.left.width.mas_equalTo(self);
        }];
    }
    else{
        
        /**
         备注
         */
        [self.remarkBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.chargeSwitchView.mas_bottom);
            make.left.width.mas_equalTo(self);
        }];
    }
    
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    self.scrollView.contentSize = CGSizeMake(self.width, self.remarkBtnView.bottom + 10 + adaptNavagationBar);
}


/**
 是否收费
 */
-(void)chargeSwitchAction:(UISwitch *)sender{
    
    UISwitch *switchButton = (UISwitch*)sender;
    
    Boolean isOn = [switchButton isOn];
    
    if (isOn) {
        
        /**
         收费
         */
        CommonInputView *commonInputView = [[CommonInputView alloc] init];
        commonInputView.titleLabel.text = @"收费(元)";
        commonInputView.titleTF.placeholder = @"请输入收费金额";
        [self.scrollView addSubview:commonInputView];
        self.commonInputView = commonInputView;
        
        /**
         支付方式
         */
        CommonBtnView *payStyleBtnView = [[CommonBtnView alloc] init];
        payStyleBtnView.titleLabel.text = @"支付方式";
        payStyleBtnView.choseLabel.text = @"请选择支付方式";
        payStyleBtnView.choseLabel.textColor = PlaceHoldColor;
        [payStyleBtnView.choseBtn addTarget:self action:@selector(payStyleBtnClick)];
        [self.scrollView addSubview:payStyleBtnView];
        self.payStyleBtnView = payStyleBtnView;
    }
    else{
        
        /**
         收费
         */
        [self.commonInputView removeFromSuperview];
        
        /**
         支付方式
         */
        [self.payStyleBtnView removeFromSuperview];
    }
    
    [self setConstraints];
    
}


#pragma mark PayStyleBtnClick
-(void)payStyleBtnClick{
    
    if ([self.choseBtnDelegate respondsToSelector:@selector(chosePayBtnClick)]) {
        
        [self.choseBtnDelegate chosePayBtnClick];
    }
}
@end
